﻿using AngleSharp.Dom;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Distributed;
using SqlSugar;
using ztDataNet.db;

namespace ztDataNet.Controllers
{
    /// <summary>
    /// 功法登记
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    public class CityController : BaseController<City>
    {
        private readonly ISqlSugarClient db;
        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="db"></param>
        /// <param name="cc"></param>
        public CityController(ISqlSugarClient db, IDistributedCache cc) : base(db, cc)
        {
            this.db = db;
        }
        public static int GenerateRandomNumber(int min, int max)
        {
            // 创建一个Random实例
            Random random = new Random();

            // Random.Next(min, max) 返回一个介于min（含）和max（不含）之间的随机整数
            // 如果你需要包含max，则需要调用Next(min, max+1)
            int randomNumber = random.Next(min, max + 1);

            return randomNumber;
        }
        /// <summary>
        /// 根据id查询单条
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        // GET: api/Entity/Get/{id}
        [HttpGet("GetCity")]
        public async Task<IActionResult> GetCity(string city)
        {

            // 如果缓存中没有数据，从数据库中获取数据
           
                var r = db.Queryable<City>().Where(x=>x.city == city).Single();
                if (r == null)
                {
                    City city1 = new City();
                    city1.city = city;
                    city1.inputtime = DateTime.Now;
                    city1.min = GenerateRandomNumber(9,14);
                    city1.max = GenerateRandomNumber(14, 24);
                    r= db.Insertable(city1).ExecuteReturnEntity();
                }
                return Ok(r);
            

        }
    }
}
